//
//  CollectionFlexLayoutVC.swift
//  Example
//
//  Created by remy on 2019/11/14.
//  Copyright © 2019 com.Risac. All rights reserved.
//

class CollectionFlexLayoutVC: BaseVC {
    private lazy var pack: CollectionPack = CollectionPack(frame: self.bodyViewFrame)
    private lazy var adjustLabel: UILabel = UILabel(frame: CGRect(x: 0.0, y: 350.0, width: 100.0, height: 20.0), textColor: FlatColor.midnightBlue, fontSize: 14.0, bold: true, bgColor: FlatColor.orange, alignment: .center)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text(title)
        topBarView.rightItems = [.text("切换方向") {
            $0.margin.right = 16.0
            $0.action(UITapGestureRecognizer(target: self, action: #selector(self.reloadTap)))
        }]
        pack.view.backgroundColor = FlatColor.greenSea
        pack.layout = CollectionFlexLayout(model: pack.model, adjustSize: true)
        self.view.addSubview(pack.view)
        let sections: [CollectionFlexLayoutSectionItem] = (0..<3).map { (index) in
            let item = CollectionFlexLayoutSectionItem()
            item.cellItems = (0..<10).map {
                let item = CollectionFlexLayoutItem()
                if $0 == 0 {
                    item.cellSize = CGSize(width: 150.0 * CGFloat(index + 1), height: 20.0 * CGFloat(3 - index))
                }
                if $0 % 3 == 0 {
                    item.cellMargin = UIEdgeInsets(top: 0.0, left: 20.0, bottom: 50.0, right: 0.0)
                }
                return item
            }
            return item
        }
        pack.model.set(sections)
        pack.view.reloadData()
        // adjust
        adjustLabel.text = "adjust: \((pack.layout as! CollectionFlexLayout).adjustSize)"
        self.view.addSubview(adjustLabel)
    }
    
    @objc private func reloadTap() {
        if pack.direction == .vertical {
            pack.layout = CollectionFlexLayout(model: pack.model, adjustSize: !(pack.layout as! CollectionFlexLayout).adjustSize, direction: .horizontal)
        } else {
            pack.layout = CollectionFlexLayout(model: pack.model, adjustSize: (pack.layout as! CollectionFlexLayout).adjustSize)
        }
        pack.view.contentOffset = .zero
        pack.view.reloadData()
        adjustLabel.text = "adjust: \((pack.layout as! CollectionFlexLayout).adjustSize)"
    }
}

class CollectionFlexLayoutSectionItem: CollectionSectionItem {
    override init() {
        super.init()
        sectionStageClass = CollectionFlexLayoutStage.self
        lineSpacing = 10.0
        itemSpacing = 2.0
        sectionSpacing = 20.0
        sectionMargin = UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)
        sectionInset = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0)
    }
}

class CollectionFlexLayoutStage: CollectionSection {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = FlatColor.amethyst
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateSection(sectionItem: CollectionSectionItem?) {}
}

class CollectionFlexLayoutItem: CollectionCellItem {
    override init() {
        super.init()
        cellClass = CollectionFlexLayoutCell.self
        cellSize = CGSize(width: CGFloat(arc4random() % 80 + 20), height: CGFloat(arc4random() % 80 + 20))
    }
}

class CollectionFlexLayoutCell: CollectionCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.backgroundColor = FlatColor.asbestos
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateCell(cellItem: CollectionCellItem?) {}
}
